package jmine.tec.scheduling.quartz;

import java.util.concurrent.Callable;

import jmine.tec.scheduling.ScheduledTaskManager;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import bancosys.tec.persist.controller.TransactionalController;
import bancosys.tec.rtm.exception.ExceptionPersister;

/**
 * A quartz job that calls the scheduled task manager, synchronizing all scheduled task timers with their persistent state.
 * 
 * @author ricardo.abe
 */
public class ScheduledTaskManagerJob extends QuartzJobBean {
    private ScheduledTaskManager manager;

    private TransactionalController controller;

    private ExceptionPersister exceptionPersister;

    /**
     * {@inheritDoc}
     */
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        try {
            this.controller.executeTransacted(new Callable<Void>() {
                public Void call() throws Exception {
                    ScheduledTaskManagerJob.this.manager.synchronizeTimers();
                    return null;
                }
            });
        } catch (Exception e) {
            this.exceptionPersister.monitor(e);
        }
    }

    /**
     * @param exceptionPersister the exceptionPersister to set
     */
    public void setExceptionPersister(ExceptionPersister exceptionPersister) {
        this.exceptionPersister = exceptionPersister;
    }

    /**
     * Setter do controller.
     * 
     * @param controller o controller.
     */
    public void setController(TransactionalController controller) {
        this.controller = controller;
    }

    /**
     * @param manager the manager to set
     */
    public void setManager(ScheduledTaskManager manager) {
        this.manager = manager;
    }
}
